package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.mapper.TbTypeTemplateMapper;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.ItemCatService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Autowired
    private TbTypeTemplateMapper tbTypeTemplateMapper;


    @Override
    public List<TbTypeTemplate> itemCatList() {
        return tbTypeTemplateMapper.selectByExample(null);
    }

    @Override
    public List<TbItemCat> getItemInfo(Long id) {
        return tbItemCatMapper.getItemInfo(id);
    }

    @Override
    public void addItem(TbItemCat tbItemCat) {
        tbItemCatMapper.insertSelective(tbItemCat);
    }

    @Override
    public TbItemCat updateById(Long id) {
        return tbItemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateItem(TbItemCat tbItemCat) {
        tbItemCatMapper.updateByPrimaryKeySelective(tbItemCat);
    }

    @Override
    public void deleteItem(Long[] id) {
        for (int i = 0; i < id.length; i++) {
            getList(id[i]);
        }
    }

    @Override
    public Long getTypeId(Long id) {
        Long typeId = tbItemCatMapper.getTypeId(id);
        return typeId;
    }


    private void getList(Long id) {
        //根据id查询
        List<TbItemCat> list = tbItemCatMapper.getItemInfo(id);
        //删除
        tbItemCatMapper.deleteByPrimaryKey(id);
        for (TbItemCat tbItemCat : list) {
            //循环递归方法传递查出的id
            getList(tbItemCat.getId());
        }
        return;
    }

    @Override
    public TbItemCat selectByType(Long category1Id) {

        return tbItemCatMapper.selectByPrimaryKey(category1Id);
    }
}
